package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.IndicatorMapper;
import cn.com.thinvent.dispatch.entity.Indicator;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.IndicatorService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class IndicatorServiceImpl implements IndicatorService {
    @Resource
    private IndicatorMapper indicatorMapper;

    @Override
    public PagedGridResult<Indicator> list(String name, Integer pageNum, Integer pageSize) {
        PagedGridResult<Indicator> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<Indicator> list = indicatorMapper.list(name, pageBegin, pageSize);
        Long count = indicatorMapper.count(name);
        pagedGridResult.setRows(list);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        return pagedGridResult;
    }

    @Override
    public Indicator saveOrUpdate(Indicator indicator) {
        Date date = new Date();
        if(indicator.getId() == null) {
            indicator.setCreateTime(date);
            indicator.setUpdateTime(date);
            indicatorMapper.insert(indicator);
        } else {
            indicator.setUpdateTime(date);
            indicatorMapper.update(indicator);
        }
        return indicator;
    }
    @Override
    public Integer delete(Integer id) {
//        List<ModelTaskTargetMapping> modelTaskTargetMapping = modelTaskTargetMappingMapper.getListByTargetId(id);
//        if(modelTaskTargetMapping.isEmpty()){
//            return modelTargetMapper.deleteById(id);
//        }else{
//            return 0;
//        }
        return indicatorMapper.deleteById(id);
    }
}
